package cn.iocoder.yudao.module.biz.controller.admin.trainingPlan;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.biz.controller.admin.trainingPlan.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.trainingPlan.BizTrainingPlanDO;
import cn.iocoder.yudao.module.biz.convert.trainingPlan.BizTrainingPlanConvert;
import cn.iocoder.yudao.module.biz.service.trainingPlan.BizTrainingPlanService;

@Api(tags = "管理后台 - 培训计划")
@RestController
@RequestMapping("/biz/training-plan")
@Validated
public class BizTrainingPlanController {

    @Resource
    private BizTrainingPlanService trainingPlanService;

    @PostMapping("/create")
    @ApiOperation("创建培训计划")
    @PreAuthorize("@ss.hasPermission('biz:training-plan:create')")
    public CommonResult<String> createTrainingPlan(@Valid @RequestBody BizTrainingPlanCreateReqVO createReqVO) {
        return success(trainingPlanService.createTrainingPlan(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新培训计划")
    @PreAuthorize("@ss.hasPermission('biz:training-plan:update')")
    public CommonResult<Boolean> updateTrainingPlan(@Valid @RequestBody BizTrainingPlanUpdateReqVO updateReqVO) {
        trainingPlanService.updateTrainingPlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除培训计划")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:training-plan:delete')")
    public CommonResult<Boolean> deleteTrainingPlan(@RequestParam("id") String id) {
        trainingPlanService.deleteTrainingPlan(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得培训计划")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:training-plan:query')")
    public CommonResult<BizTrainingPlanRespVO> getTrainingPlan(@RequestParam("id") String id) {
        BizTrainingPlanDO trainingPlan = trainingPlanService.getTrainingPlan(id);
        return success(BizTrainingPlanConvert.INSTANCE.convert(trainingPlan));
    }

    @GetMapping("/list")
    @ApiOperation("获得培训计划列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:training-plan:query')")
    public CommonResult<List<BizTrainingPlanRespVO>> getTrainingPlanList(@RequestParam("ids") Collection<String> ids) {
        List<BizTrainingPlanDO> list = trainingPlanService.getTrainingPlanList(ids);
        return success(BizTrainingPlanConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得培训计划分页")
    @PreAuthorize("@ss.hasPermission('biz:training-plan:query')")
    public CommonResult<PageResult<BizTrainingPlanRespVO>> getTrainingPlanPage(@Valid BizTrainingPlanPageReqVO pageVO) {
        PageResult<BizTrainingPlanDO> pageResult = trainingPlanService.getTrainingPlanPage(pageVO);
        return success(BizTrainingPlanConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出培训计划 Excel")
    @PreAuthorize("@ss.hasPermission('biz:training-plan:export')")
    @OperateLog(type = EXPORT)
    public void exportTrainingPlanExcel(@Valid BizTrainingPlanExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BizTrainingPlanDO> list = trainingPlanService.getTrainingPlanList(exportReqVO);
        // 导出 Excel
        List<BizTrainingPlanExcelVO> datas = BizTrainingPlanConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "培训计划.xls", "数据", BizTrainingPlanExcelVO.class, datas);
    }

}
